package com.firstjianmo.listviewtest;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by pc on 2016/11/30.
 */

public class ListItemAdapter extends ArrayAdapter<ListItem> {

    private int resourceId;

    private Set<View> viewSet = new HashSet<>();

    public ListItemAdapter(Context context, int resourceId, List<ListItem> objects) {
        super(context, resourceId, objects);
        this.resourceId = resourceId;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ListItem listItem = getItem(position);
        View view;
        ViewHolder viewHolder;
        if (null == convertView) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder();
            viewHolder.image = (ImageView) view.findViewById(R.id.image);
            viewHolder.msg = (TextView) view.findViewById(R.id.msg);
            view.setTag(viewHolder); // 将ViewHolder存入View之中, 当使用时 直接通过getTag()来获取即可
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.image.setImageResource(listItem.getImageId());
        viewHolder.msg.setText(listItem.getMsg());
        return view;
    }

    class ViewHolder{
        ImageView image;
        TextView msg;
    }


    private void checkViewIsNew(View view){
        int oldSetSize = viewSet.size();
        viewSet.add(view);
        int newSetSize = viewSet.size();
        if (oldSetSize < newSetSize) {
            Log.e("新增了","总共View数量" + newSetSize);
        }
    }

}
